/* QAD_WipAdjustmentsAnalysisReport.p - Report Proxy Template                 */
/* Copyright 1986-2010 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy rewarp01.p for use by QAD Reporting Framework.       */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_op_hist no-undo like op_hist
   field ptal_alt_part  like ptal_mstr.ptal_alt_part
   field qty_cumproc    like wr_route.wr_qty_cumproc
   field rsn_desc       like rsn_ref.rsn_desc
   field wr_qty_cumoadj like wr_route.wr_qty_cumoadj
   field percent_adjust as   decimal
.

define temp-table ttReportConditions no-undo
   field wo_lot    like wo_mstr.wo_lot
   field wo_part   like wo_mstr.wo_part
   field wo_site   like wo_mstr.wo_site
   field wo_line   like wo_mstr.wo_line
   field wc_wkctr  like wc_mstr.wc_wkctr
   field wc_mch    like wc_mstr.wc_mch
   field wc_dept   like wc_mstr.wc_dept
   field effdate   as   date
   field order_type as   character
.

define dataset dsReportResults for tt_op_hist, ttReportConditions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.
define variable orderType as character no-undo.
define  variable order_type_key  like lngd_key1 initial "0" no-undo.

empty temp-table tt_op_hist         no-error.
empty temp-table ttReportConditions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_WipAdjustmentsAnalysisReport.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle(
         "ttReportConditions", "wo_site", "DefaultValue", global_site). 

   for first gl_ctrl  where gl_ctrl.gl_domain = global_domain no-lock:
   end.

   for first lngd_det where lngd_dataset = c-lngd-dataset
      and lngd_field   = "orderType"
      and lngd_lang    = global_user_lang
      and lngd_key1    = order_type_key
   no-lock:
   end.
   if available lngd_det then do:
      orderType = lngd_key2.
   end.
   else do:
      orderType = "".
   end.

   run SetFieldMetaParameter in ipReportHandle(
         "ttReportConditions", "order_type", "DefaultValue", orderType).

   
  /*
   run SetFieldMetaParameter in ipReportHandle(
         "ttCoByProductsCostRpt", "si_site", "DefaultValue", site).
   */

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for so_mstr.

   assign
      hMainQuery           = query MainQuery:handle.

   for first lngd_det where
      lngd_dataset     = c-lngd-dataset
      and lngd_field       = "orderType"
      and lngd_lang        = global_user_lang
      and lngd_key2 = orderType
   no-lock:
   end.
   if not available lngd_det
   then do:
      /* MUST BE (S)cheduled or (D)iscrete or (B)oth */
      {us/bbi/pxmsg.i &MSGNUM=10346 &ERRORLEVEL=4}
      return.
   end.
   order_type_key=lngd_key1.

   queryString = "for each so_mstr no-lock where so_domain = "
                  + quoter(global_domain).

   run AddAllConditions in reportHandle
      ("so_mstr", input-output queryString).

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
